﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace LiveatEdu.dao
{
    class ConnectionTXT
    {
        private FileStream lofFile;
        private TextWriter txtWriter;

        public ConnectionTXT() { }

        public ConnectionTXT(string pathToLog,string fileToLog,string actualDate)
        {
            string fileLog =pathToLog + fileToLog + actualDate ;
            try
            {                
                lofFile = new FileStream(fileLog + ".txt", FileMode.Append);
                this.txtWriter = new StreamWriter(this.lofFile);
            }
            catch (DirectoryNotFoundException e)
            {
                Console.WriteLine(e);
                Console.WriteLine("Diretório "+pathToLog+"  não encontrado");
                Console.ReadKey();
            }
            catch (UnauthorizedAccessException e)
            {
                Console.WriteLine(e);
                Console.WriteLine("Sem permissão de escrita no diretório "+pathToLog);
                Console.ReadKey();
            }
        }

        public void AddMSgError(String logMsg)
        {
            this.txtWriter.WriteLine(logMsg);        
        }

        public void AddMSgHeader()
        {
            this.txtWriter.WriteLine("==========COMEÇO LOG==================================================================");
            this.txtWriter.WriteLine("Start Date: "+ DateTime.Now);
        }
        
        public void AddMSgStartBlock()
        {
            this.txtWriter.WriteLine("Start Time: " + DateTime.Now);
        }
        
        public void AddMSgEndBlock()
        {
            this.txtWriter.WriteLine("End Time: " + DateTime.Now);
        }
       
        public void Close()
        {
            ConsoleLiveEdu.logFile.AddMSgError("logFile  successful close at " + DateTime.Now);
            this.txtWriter.WriteLine("==========FIM LOG=====================================================================");         
            txtWriter.Close();
            
        }

    }
}
